package com.timguan.render.dataprocessor.processor;

import com.timguan.render.dataapi.api.ICmsDataManager;
import com.timguan.render.dataapi.domain.CmsDataDO;
import com.timguan.render.dataprocessor.filter.TimeVaildFilter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询cms_data表获取当前时间在开始结束时间内的对应的数据
 * Created by guankaiqiang on 2015/3/21.
 */
public class TimeVaildDataProcessor extends DataProcessor {
    @Autowired
    private ICmsDataManager cmsDataManagerImpl;
    @Autowired
    private TimeVaildFilter timeVaildFilter;

    protected List<Object> execute(CommonParametersWrapper wrapper, Map<String, String> params) {
        if (params != null && params.get(PROCESSOR_PARAM_DATALISTID) != null) {
            int listId = Integer.valueOf(params.get(PROCESSOR_PARAM_DATALISTID));
            List<CmsDataDO> cmsDataDOs = cmsDataManagerImpl.selectByListId(listId);
            cmsDataDOs = dataFilterChain.doFilter(cmsDataDOs, new HashMap<String, String>() {
                {
                    put(START_DISPLAY_TIME, String.valueOf(System.currentTimeMillis()));
                }
            });
            return appendSpm(wrapper, cmsDataDOs, Constants.SPM_START);
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //使用时需要创建数据列表
        needCreateDatalistWhenUsed = true;
        description = "排期";
        dataFilterChain.add(timeVaildFilter);
    }
}
